package com.hardydou.pm.controller;

import com.github.pagehelper.PageInfo;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.Result;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.framework.log.anno.LogMetaInfo;
import com.hardydou.pm.model.PmTask;
import com.hardydou.pm.service.IPmTaskService;
import com.hardydou.system.model.SystemUser;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：任务控制器
 * @date ：Created in 2020/10/23
 * @Copyright ：Hardydou.com
 */
@Controller
@RequestMapping("/pm/task")
@Api(tags = "任务管理")
@LogMetaInfo(module = "任务管理")
public class TasksController extends BaseController {

    public final static String model = "pm";
    public final static String subModel = "task";

    @Autowired
    IPmTaskService taskService;


    @ApiIgnore
    @GetMapping("")
    public String index() {
        return page("list");
    }


    @ApiIgnore
    @GetMapping("/add")
    public String add() {
        return page("add");
    }


    @ApiIgnore
    @PostMapping("/add")
    public String add(PmTask pmTask, Model model) {
        SystemUser user =getLoginUserDetails().getSystemUser();
        pmTask.setUser(user);
        taskService.insert(pmTask);
        model.addAttribute("code", 200);
        model.addAttribute("message", "添加成功");
        return page("add");
    }

    @ApiIgnore
    @GetMapping("/edit")
    public String edit(Integer taskId, Model model) {
        PmTask pmTask = taskService.selectByPrimaryKey(taskId);
        model.addAttribute("task", pmTask);
        return page("edit");
    }

    @ApiIgnore
    @PutMapping("/edit")
    public String edit(PmTask pmTask, Model model) {
        model.addAttribute("code", 200);
        model.addAttribute("message", "修改成功");
        taskService.updateByPrimaryKeySelective(pmTask);
        model.addAttribute("task", pmTask);
        return page("edit");
    }


    @ApiIgnore
    @GetMapping("/info")
    public String info(Integer taskId, Model model) {
        PmTask pmTask = taskService.selectByPrimaryKey(taskId);
        model.addAttribute("task", pmTask);
        return page("info");
    }


    /* JSON API */
    @ApiOperation(value = "任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listForJson(@RequestParam Map params) {
        List<PmTask> pmTasks = taskService.list(params);
        PageInfo pageInfo = new PageInfo(pmTasks);
        return ResultHelper.ok(pageInfo);
    }


    @ApiOperation(value = "添加任务")
    @PostMapping("/add.json")
    @ResponseBody
    public Result<?> addForJson(PmTask pmTask) {
        taskService.insert(pmTask);
        return ResultHelper.ok(pmTask);
    }


    @ApiOperation(value = "删除任务")
    @ApiImplicitParam(name = "taskId", value = "任务ID", required = true)
    @DeleteMapping("/del")
    @ResponseBody
    public Result delete(Integer taskId) {
        taskService.deleteByPrimaryKey(taskId);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("taskId", taskId);
        return result;
    }

    @ApiOperation(value = "更新任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID", required = true, paramType = "query")
    })
    @ApiParam(name = "taskId", value = "任务ID", required = true)
    @PutMapping("/update")
    @ResponseBody
    public Result update(PmTask pmTask) {
        taskService.updateByPrimaryKeySelective(pmTask);
        return ResultHelper.ok(pmTask);
    }

    @Override
    public String getModel() {
        return model;
    }

    @Override
    public String getSubModel() {
        return subModel;
    }

}
